import sys
from pathlib import Path

from PyQt5.QtCore import QPoint, Qt, pyqtSignal
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QApplication, QHBoxLayout, QVBoxLayout, QFrame
from qfluentwidgets import (CardWidget, IconWidget, CaptionLabel, LineEdit, LineEditButton, FluentIcon,
                            InfoBar, PushButton, ImageLabel, TitleLabel, SubtitleLabel, TransparentToolButton,
                            BodyLabel)

from tools import FileInformation


class AppCard(CardWidget):
    """ App card """

    def __init__(self, icon, title, content, parent=None):
        super().__init__(parent)
        self.iconWidget = IconWidget(icon)
        self.titleLabel = BodyLabel(title, self)
        self.contentLabel = CaptionLabel(content, self)

        self.hBoxLayout = QHBoxLayout(self)
        self.vBoxLayout = QVBoxLayout()

        self.setFixedSize(300, 73)
        self.iconWidget.setFixedSize(48, 48)
        self.contentLabel.setTextColor("#606060", "#d2d2d2")

        self.hBoxLayout.setContentsMargins(20, 11, 11, 11)
        self.hBoxLayout.setSpacing(15)
        self.hBoxLayout.addWidget(self.iconWidget)

        self.vBoxLayout.setContentsMargins(0, 0, 0, 0)
        self.vBoxLayout.setSpacing(0)
        self.vBoxLayout.addWidget(self.titleLabel, 0, Qt.AlignVCenter)
        self.vBoxLayout.addWidget(self.contentLabel, 0, Qt.AlignVCenter)
        self.vBoxLayout.setAlignment(Qt.AlignVCenter)
        self.hBoxLayout.addLayout(self.vBoxLayout)

        self.hBoxLayout.addStretch(1)


class SaveCard(QFrame):
    clicked = pyqtSignal()
    saved = pyqtSignal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self.setFixedHeight(50)

        self.hBox = QHBoxLayout(self)
        self.hBox.setContentsMargins(0, 0, 0, 0)
        self.hBox.setSpacing(10)

        self.lineEdit = LineEdit()
        self.lineEdit.setFixedHeight(30)
        self.openFileButton = LineEditButton(FluentIcon.FOLDER, self.lineEdit)
        self.lineEdit.hBoxLayout.addWidget(self.openFileButton)
        self.lineEdit.setClearButtonEnabled(True)
        self.lineEdit.setPlaceholderText('请选择文件保存路径..')
        self.openFileButton.clicked.connect(self.saved.emit)

        self.addButton = PushButton()
        self.addButton.setFixedSize(100, 30)
        self.addButton.clicked.connect(self.clicked.emit)

        self.setFixedHeight(30)

        self.hBox.addWidget(self.lineEdit)
        self.hBox.addWidget(self.addButton)

    def addWidget(self, widget):
        self.hBox.addWidget(widget)

    def setLineText(self, text):
        self.lineEdit.setText(text)

    def lineText(self):
        text = self.lineEdit.text()
        if text == '':
            InfoBar.warning('提示', '请选择保存路径', parent=self.parent())
        return text

    def setButtonText(self, text):
        self.addButton.setText(text)

    def buttonText(self):
        return self.addButton.text()

    def setIcon(self, icon):
        self.addButton.setIcon(icon)


class ImgCard(QFrame):
    closed = pyqtSignal(TransparentToolButton)

    def __init__(self, filename: str, parent=None):
        super().__init__(parent)
        self.setFixedHeight(140)

        path = FileInformation(filename)
        self.imageLabel = ImageLabel()
        self.setImage(filename)

        self.nameLabel = TitleLabel(path.name(), self)
        self.sizeLabel = SubtitleLabel(f'大小：{path.size()}', self)
        self.fileLabel = BodyLabel(f'完整路径：{filename}', self)

        self.deleteButton = TransparentToolButton(FluentIcon.CLOSE, self)
        self.deleteButton.setFixedSize(40, 40)
        self.deleteButton.clicked.connect(lambda: self.closed.emit(self.deleteButton))

        self.__initLayout()

    def __initLayout(self):
        self.hBox = QHBoxLayout(self)
        self.hBox.setContentsMargins(10, 10, 10, 10)
        self.hBox.setSpacing(10)
        self.hBox.setAlignment(Qt.AlignmentFlag.AlignVCenter)

        frame = QFrame()
        vBox = QVBoxLayout(frame)
        vBox.setContentsMargins(0, 0, 0, 0)
        vBox.setSpacing(5)
        vBox.setAlignment(Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignLeft)
        vBox.addWidget(self.nameLabel)
        vBox.addWidget(self.sizeLabel)
        vBox.addWidget(self.fileLabel)

        self.hBox.addWidget(self.imageLabel)
        self.hBox.addWidget(frame, 0, Qt.AlignmentFlag.AlignLeft)
        self.hBox.addWidget(self.deleteButton, 0, Qt.AlignmentFlag.AlignRight)

    def setImage(self, file: str):
        self.imageLabel.setImage(
            QImage(file).scaled(
                120, 120,
                Qt.AspectRatioMode.KeepAspectRatioByExpanding,
                Qt.TransformationMode.SmoothTransformation
            )
        )

    def text(self) -> str:
        return self.fileLabel.text().replace('完整路径：', '')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    filename = r"F:\PythonObject\office-tools\resource\image\access.svg"
    w = ImgCard(filename)
    w.show()
    sys.exit(app.exec())
